package com.htudata.messager.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.messager.mapper.MessagerMapper;
import com.htudata.messager.service.IMessagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessagerServiceImpl implements IMessagerService {

  @Autowired
  private MessagerMapper messagerMapper;

  @Override
  public JSONObject selectServChannelUserStatis()  throws Exception {
    try {
      JSONArray servChannelUserStatis = this.messagerMapper.selectServChannelUserStatis();
      List<JSONObject> servChannelUserStatisList = null;
      JSONObject jsonObject = new JSONObject();
      Map<Object, Integer> servChannelUserStatisMap = null;
      if (servChannelUserStatis != null) {
        servChannelUserStatisList = (List<JSONObject>)JSON.parse(servChannelUserStatis.toJSONString());
        servChannelUserStatisMap = servChannelUserStatisList.stream().collect(Collectors.groupingBy(x -> x.getString("name"), Collectors.summingInt(x -> x.getInteger("value").intValue())));
        jsonObject.put("servChannelUserStatis", servChannelUserStatisMap);
        jsonObject.put("units", ((JSONObject)servChannelUserStatisList.get(0)).getString("units"));
      } 
      return jsonObject;
    } catch (Exception e) {
       throw e;
    } 
  }

  @Override
  public JSONObject selectServChannelActiveStatis() {
    try {
      int year = LocalDateTime.now().getYear();
      int month = LocalDateTime.now().getMonthValue();
      String yearMonth = year + "" + month;
      JSONArray servChannelActiveStatis = this.messagerMapper.selectServChannelActiveStatis(yearMonth);
      List<JSONObject> servChannelActiveStatisList = null;
      JSONObject jsonObject = new JSONObject();
      Map<String, List<JSONObject>> servChannelStatisMap = null;
      if (servChannelActiveStatis != null)
        servChannelActiveStatisList = (List<JSONObject>)JSON.parse(servChannelActiveStatis.toJSONString()); 
      jsonObject.put("servChannelActiveStatis", servChannelActiveStatisList);
      return jsonObject;
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectServChannelStatis(String servChannel) throws Exception {
    try {
      int year = LocalDateTime.now().getYear();
      return this.messagerMapper.selectServChannelStatis(servChannel, year);
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectMeteoPublicUserComment()  throws Exception{
    return this.messagerMapper.selectMeteoPublicUserComment();
  }

  @Override
  public JSONObject selectServChannelData(String servChannel)  throws Exception{
    JSONObject jsonObject = new JSONObject();
    JSONObject servChannelUserStatis = selectServChannelUserStatis();
    JSONObject servChannelActiveStatis = selectServChannelActiveStatis();
    JSONArray servChannelStatis = selectServChannelStatis(servChannel);
    List<JSONObject> servChannelStatisList = null;
    Map<String, List<JSONObject>> servChannelStatisMap = null;
    if (servChannelStatis != null) {
      servChannelStatisList = (List<JSONObject>)JSON.parse(servChannelStatis.toJSONString());
      servChannelStatisMap = (Map<String, List<JSONObject>>)servChannelStatisList.stream().collect(Collectors.groupingBy(x -> x.getString("item")));
    } 
    jsonObject.put("servChannelUserStatis", servChannelUserStatis);
    jsonObject.put("servChannelActiveStatis", servChannelActiveStatis);
    jsonObject.put("servChannelStatis", servChannelStatisMap);
    return jsonObject;
  }

  @Override
  public String selectServChannelDataJSON(String servChannel)  throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_017.getCode(), selectServChannelData(servChannel));
    } catch (Exception e) {
      throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public String selectMeteoPublicUserCommentJSON()  throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_018.getCode(), selectMeteoPublicUserComment());
    } catch (Exception e) {
      throw e;
    } 
    return JSON.toJSONString(result);
  }
}
